﻿/// <reference path="../../../amplify-vsdoc.js" />
/// <reference path="../../../amplify.js" />
YUI.add('agregarDocente-curso-view', function (Y) {
    'use strict';
    Y.AgregarDocenteCursoView = Y.Base.create('AgregarDocenteCursoView', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/calificaciones/cursos/templates/asistenteSeleccionarDocente.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response)
                    }
                })
            }
        },
        events: {
            '.save': {
                click: 'guardarCurso'
            },
            '.backEvent': {
                click: 'regresarADatosCurso'
            },
            'cancelEvent': {
                click: 'cancelar'
            }
        },

        render: function () {
            this.loadTemplate();
            if (this.templateIsCharged) {
                this.get('container').setHTML(this.template);

                //carga de docentes
                var docentes = new Y.Docentes();
                var that = this, a = docentes.load({ urlName: 'listPaging' }, function () {

                    var table = $('#table_id').DataTable({
                        "language": {
                            "search": "Buscar:",
                            "lengthMenu": "Items por página: _MENU_",
                            "info": "Items del _START_ al _END_ de _TOTAL_ items.",
                            "emptyTable": "Items no disponibles.",
                            "infoEmpty": "0 ítems.",
                            "paginate": {
                                "next": "Siguiente",
                                "previous": "Anterior",
                                "first": "Primero",
                                "last": "Ultimo"
                            }
                        },
                        "paging": true,
                        retrieve: true,
                        data: docentes._items,
                        "rowCallback": function (row, aData) {
                            $('td:eq(6)', row).html('<a class="btn btn-info save"  id="' + aData.get('Id') + '" >Seleccionar</a>');
                        },
                        columns: [
                            { data: '_state.data.Id.lazy.value' },
                            { data: '_state.data.Nombre.lazy.value' },
                            { data: '_state.data.Apellido.lazy.value' },
                            { data: '_state.data.Direccion.lazy.value' },
                            { data: '_state.data.Telefono.lazy.value' },
                            { data: '_state.data.Correo.lazy.value' },
                            { data: '_state.data.Id.lazy.value' }
                        ]
                    });
                    table.draw();
                });
                Y.one('#navigationButton').set('href', '/cursos/' + amplify.store('cursoModel').IdCurso)
            }
        },

        guardarCurso: function (ev) {

            debugger


            var cursoModel = new Y.Curso(amplify.store('cursoModel'));

            cursoModel.set('IdDocente', ev.currentTarget.get('id'));
            var options = null;

            if (cursoModel.get('Id') != 0 && cursoModel.get('Id') != undefined)
                options = { action: 'update', idEst: cursoModel.get('Id') };
            else
                options = { urlName: 'saveOption' };


            var those = this, newModel = cursoModel.save(options, function (err, response) {
                if (err != null)
                    bootbox.dialog({
                        message: "Ocurrieron problemas al asignar el docente",
                        title: "Problemas al registrar el Docente",
                        show: true,
                        backdrop: true,
                        closeButton: true,
                        animate: true,

                        className: "my-modal",
                        buttons: {
                            success: {
                                label: "Ok",
                                className: "btn btn-danger",
                                callback: function () { }
                            }
                        }
                    });
                else {

                    //cursoModel.set('Id', Y.JSON.parse(response.responseText).Id)
                    //amplify.store('cursoModel', Y.JSON.stringify(cursoModel))


                    bootbox.dialog({
                        message: "<h5>Curso Registrado</h5>",
                        title: "Confirmación",
                        show: true,
                        backdrop: true,
                        closeButton: true,
                        animate: true,
                        className: "my-modal",
                        buttons: {
                            success: {
                                label: "Ok",
                                className: "btn btn-success",
                                callback: function () { }
                            }
                        }
                    });
                    Y.one('#navigationButton').set('href', '/cursos')
                    Y.one('#navigationButton').simulate("click");
                }


            })

        },

        regresarADatosCurso: function (ev) {
            if (ev.preventDefault)
                ev.preventDefault();
            else
                ev.returnValue = false;
            Y.one('#navigationButton').set('href', '/curso/' + this.get('model').get('Id'))
            Y.one('#navigationButton').simulate("click");
        },
        cancelar: function (ev) {
            if (ev.preventDefault)
                ev.preventDefault();
            else
                ev.returnValue = false;
            localStorage.removeItem('cursoModel')
            Y.one('#navigationButton').set('href', '/cursos')
            Y.one('#navigationButton').simulate("click");
        }
    })

}, '0.0.0.1', { requires: ['view', 'handlebars', 'node', 'event', 'node-event-simulate', 'json-stringify', 'json-parse', 'curso-model'] })